﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ScrumTrackerAjax.Models;
using System.Web.Security;
using ScrumTracker.Model;
using System.Security.Cryptography;

namespace ScrumTrackerAjax.Controllers
{
    public class AccountsController : AbstractController
    {
        //
        // GET: /Accounts/
        public ActionResult LogIn()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogIn(AccountViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = ScrumTracker.Model.User.Authenticate(model.Email, model.Password);
                if (user != null)
                {
                    FormsAuthentication.SetAuthCookie(model.Email, false);
                    return Redirect(Request["ReturnUrl"] ?? "~");
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Invalid credentials");
                    return View();
                }
            }
            else
            {
                return View();
            }
        }

        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();
            return Redirect("~");
        }
    }
}
